package org.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TimerWithGUI {

    private JFrame frame;
    private JLabel timeLabel;
    private JButton startButton;
    private JButton stopButton;
    private JTextField textField;
    private ScheduledExecutorService scheduler;
    private boolean running;
    private long[] elapsedTime = {0};
    public TimerWithGUI() {
        frame = new JFrame("Timer with GUI");
        timeLabel = new JLabel("00:00", SwingConstants.CENTER);
        startButton = new JButton("Start");
        stopButton = new JButton("Stop");
        running = false;

        // 创建JTextField实例
        textField = new JTextField(20);

        // Set up the layout
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(textField, BorderLayout.NORTH);
        panel.add(timeLabel, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);


        frame.getContentPane().add(panel);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        // Add button listeners
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!running) {
                    startTimer();
                }
            }
        });

        stopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                stopTimer();
            }
        });
    }
    private void setTimeLabel() {
        int minutes = (int) (elapsedTime[0] / 60000);
        int seconds = (int) ((elapsedTime[0] % 60000) / 1000);
        timeLabel.setText(String.format("%02d:%02d", minutes, seconds));
    }
    private void startTimer() {
        running = true;
        scheduler = Executors.newSingleThreadScheduledExecutor();
//        final long[] elapsedTime = {0}; // Use an array to hold the mutable long value

        elapsedTime[0] = Integer.parseInt(textField.getText()) * 1000L; // Convert input string to milliseconds
        setTimeLabel();
        scheduler.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                elapsedTime[0] -= 1000; // Increment by 1 second
                setTimeLabel();
                if (elapsedTime[0] <= 0) {
                    stopTimer();
                    // 创建一个简单的警告对话框
                    JOptionPane.showMessageDialog(null, "时间到！", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }


        }, 0, 1, TimeUnit.SECONDS);
    }

    private void stopTimer() {
        running = false;
        if (scheduler != null) {
            scheduler.shutdownNow();
            scheduler = null;
        }
        timeLabel.setText("00:00"); // Reset the label
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TimerWithGUI();
            }
        });
    }
}
